<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class TradePage extends CI_Controller {
	
	public function index()
	{      
             $isLogin = $this->session->userdata('is_login');
            if(isset($isLogin)){
                $hasSession=$isLogin;
                $learnid=$this -> input -> post("learnid",TRUE);
                $teachid=$this-> input -> post("teachid",TRUE);
                $epid=$this-> input -> post("epid",TRUE);
                // user is currently login
                $this->load->model('user');
                $this->load->model('trade');
                $this->load->model('skills');
                $trades=$this->trade->get_current_trades();
                $tradelist = array();
                
                foreach($trades->result() as $trade){
                    $learnid = $trade->TO_LEARN_SKILL_ID;
                    $teachid = $trade->TO_TEACH_SKILL_ID;
                    $epid = $trade->EXPIRATION_DT;
                    $learnname = $this->skills->getNameById($learnid);
                    $teachname = $this->skills->getNameById($teachid);
                    //$epid = $this->skills->getNameById($epid);
                    $tradeSet = array(
                        'teachname'=>$teachname,
                        'learnname'=>$learnname,
                        'learnid'=>$learnid,
                        'teachid'=>$teachid,
                        'epid'=>$epid);
               array_push($tradelist, $tradeSet);// each trade result
           }
                
                $uid=$this->session->userdata("user_id");
                $curUser=$this->user->getUser($uid);
                $data['curuser']=$curUser;
                $data['tradelist']=  $tradelist;
                //$data=array("hasSession"=>$hasSession);           
                $this->load->view('tradepage',$data);                
            }else{
                $hasSession=FALSE;
            }
	}
        
        public function deleteTrade(){
            $uid = $uid = $this->session->userdata("user_id");
            if($uid!=NULL){
                $table=filter_input(INPUT_POST,'table',FILTER_SANITIZE_STRING);
                $teachid=  filter_input(INPUT_POST,'teachid',FILTER_SANITIZE_STRING);
                $learnid=  filter_input(INPUT_POST,'learnid',FILTER_SANITIZE_STRING);
                //$epid=  filter_input(INPUT_POST,'epid',FILTER_SANITIZE_STRING);
                $sql="DELETE FROM $table WHERE USER_LOGIN_ID =".$uid." AND TO_LEARN_SKILL_ID=".$learnid." AND TO_TEACH_SKILL_ID=".$teachid;//." AND EXPIRATION_DT=".$epid;
                echo $uid, $learnid, $teachid;
                $resp=$this->db->simple_query($sql);
                echo json_encode(array('success'=>$resp));
        }
    }
    
        public function inserttrade(){
 
            $teachid=filter_input(INPUT_POST,'teachid',FILTER_SANITIZE_STRING);
            $learnid=filter_input(INPUT_POST,'learnid',FILTER_SANITIZE_STRING);
            $epid = filter_input(INPUT_POST,'epid',FILTER_SANITIZE_STRING);
            $description = filter_input(INPUT_POST,'description',FILTER_SANITIZE_STRING);
            $uid=$uid = $this->session->userdata("user_id");
            if($uid!=NULL){
                
                $sql_learnlist = "SELECT SKILL_NAME FROM master_skill";
                $learnlist = $this->db->query($sql_learnlist);
                $arraylist = array();
                foreach ($learnlist->result() as $each_learnname){
                    array_push($arraylist, $each_learnname);
                }
                if(in_array($learnid, $arraylist)){
                    echo "";
                }
                else {
                    $sql_insert_newlearnid = "INSERT INTO master_skill (SKILL_NAME, SKILL_TYPE) VALUES ('$learnid', 'SOFTWARE')";
                        //echo $learnid;
                        $this->db->query($sql_insert_newlearnid);
                        $get_learnid1 = "SELECT master_skill.id, master_skill.SKILL_TYPE FROM master_skill WHERE master_skill.SKILL_NAME = '". $learnid ."'";
                        $get_learnid=$this->db->query($get_learnid1);
                        if($get_learnid->num_rows()>0){
                            foreach ($get_learnid->result() as $value) {
                                $sql_get_learnid = $value->id;  
                            }
                        }                       
                    }
                  /*if(!in_array('$learnid', $arraylist)){
                        $sql_insert_newlearnid = "INSERT INTO master_skill (SKILL_NAME, SKILL_TYPE) VALUES ('$learnid', 'SOFTWARE')";
                        //echo $learnid;
                        $this->db->query($sql_insert_newlearnid);
                        $get_learnid = "SELECT master_skill.id master_skill.SKILL_NAME FROM master_skill WHERE SKILL_NAME = '$learnid'";
                        if($get_learnid->num_rows()>0){
                            foreach ($get_learnid->result() as $value) {
                                $sql_get_learnid = $value->id;  
                            }
                        }
                        //$sql_get_learnid = $this->db->query($get_learnid)->row();
                        //$get_learnid = $sql_get_learnid['id'];
                }*/
                /*foreach ($learnlist as $learn_each) {
                    if($learn_each != $learnid){
                       
                        $sql_insert_newlearnid = "INSERT INTO master_skill (SKILL_NAME, SKILL_TYPE) VALUES ('$learnid', 'SOFTWARE')";
                        $this->db->query($sql_insert_newlearnid);
                        $get_learnid = "SELECT id FROM master_skill WHERE SKILL_NAME = $learnid";
                        $sql_get_learnid = $this->db->query($get_learnid);
                        break;
                    }*/
                    /*else {
                        $get_learnid = "SELECT id FROM master_skill WHERE SKILL_NAME = $learnid";
                        $sql_get_learnid = $this->db->query($sql_get_learnid);
                        continue;
                    }*/
               // }
                // get the name of the learn skill
                
                // intsert data to matches table
                $sql_insert_skill="INSERT INTO skills (USER_ID,SKILL_ID) VALUES (1,$sql_get_learnid)";
                $this->db->query($sql_insert_skill);
                $get_id_from_skills= $this->db->insert_id();
                $sql_get_learnid_query = "SELECT id,SKILL_ID FROM skills WHERE id=".$get_id_from_skills;
                $sql_get_learnid_query=$this->db->query($sql_get_learnid_query);
                foreach ($sql_get_learnid_query->result() as $value) {
                                $get_learnid_from_skills = $value->SKILL_ID;  
                            }
                
                $epid1 =  str_replace('/', '-', $epid);
                $newFormat= DateTime::createFromFormat('m-d-Y H:i:s', $epid1)->format('Y-m-d H:i:s');
                //$sql2 = "INSERT INTO matches (DESCRIPTION,USER_LOGIN_ID,TO_LEARN_SKILL_ID,TO_TEACH_SKILL_ID,EXPIRATION_DT) VALUES ('$description',$uid,90,$teachid,'$newFormat')";
                //$sql = "INSERT INTO matches (TO_TEACH_SKILL_ID, TO_LEARN_SKILL_ID, EXPIRATION_DT, DESCRIPTION) VALUES ($this->getteachskillsbyid($teachid),$sql_get_learnid,$epid,$description)";               
                $sql2 = "INSERT INTO matches (TO_LEARN_SKILL_ID) VALUES ($sql_get_learnid)"; //DESCRIPTION,USER_LOGIN_ID,TO_LEARN_SKILL_ID,
                $this->db->query($sql2);
                $affectedRow = $this->db->affected_rows();
                if($affectedRow >0){
                    // meaning some row has been affected
                    echo json_encode(array("success"=>TRUE));
                }else{
                    echo json_encode(array("success"=>FALSE));
                }
            }
            }       
}



